
import java.util.Arrays;

/**
 * @author JiuQi 玖柒
 * @date 2021/7/7 23:11
 */
public class T167 {
    public static void main(String[] args) {
        int[] numbers = {2, 7, 11, 32};
        int target = 9;
        System.out.println(Arrays.toString(new Solution().twoSum(numbers, target)));
    }
}

/*class Solution {
    public int[] twoSum(int[] numbers, int target) {
        int n = numbers.length;
        int[] temp = new int[2];
        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 1; j < n; j++) {
                if (numbers[i] + numbers[j] == target) {
                    temp[0] = i + 1;
                    temp[1] = j + 1;
                }
            }
        }
        return temp;
    }
}*/

//使用双指针
class Solution {
    public int[] twoSum(int[] numbers, int target) {
        int left = 0, right = numbers.length - 1;
        while (left < right) {
            int sum = numbers[left] + numbers[right];
            if (sum > target) {
                --right;
            } else if (sum < target) {
                ++left;
            } else {
                return new int[]{left + 1, right + 1};
            }
        }
        return new int[]{-1, -1};
    }
}
